package com.sbpm.crmbpm.server.dao.impl;

import com.sbpm.crmbpm.server.dao.CompanyDao;
import com.sbpm.crmbpm.shared.data.client.Company;
import com.sbpm.crmbpm.shared.data.client.Segment;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Component
public class CompanyDaoImpl extends AbstractDaoImpl<Company> implements CompanyDao
{
    //@Autowired
    @PersistenceContext()
    private EntityManager em;


    public List<Company> findCompanies( String value, int offset, int limit )
    {
        String ql = "select c from Company c where ( c.name like :value or c.inn like :value or c.legalName like :value ) ";
        Query q = em.createQuery( ql );
        return q.setMaxResults( limit )
                .setFirstResult( offset )
                .setParameter( "value", value + "%" )
                .getResultList();
    }

    public int findCompaniesCount( String value )
    {
        String ql = "select count(c) from Company c where ( c.name like :value or c.inn like :value or c.legalName like :value ) ";
        Query q = em.createQuery( ql );
        return Integer.parseInt( q.setParameter( "value", value + "%" )
                .getSingleResult().toString() );
    }

    public List<Company> findCompanies( String name, String legalName, String inn, String ogrn, String kpp, Segment segment )
    {
        String qlString = "select c from Company c where 1=1 ";
        if( name != null )
        {
            qlString += " and ( lower(c.name) like lower(:name) or lower(c.legalName) like lower(:name) )";
        }
        if( inn != null )
        {
            qlString += " and c.inn = :inn";
        }
        if( ogrn != null )
        {
            qlString += " and c.ogrn = :ogrn";
        }
        if( kpp != null )
        {
            qlString += " and c.kpp = :kpp";
        }
        if( segment != null )
        {
            qlString += " and c.segment = :segment";
        }

        Query q = em.createQuery( qlString );
        if( name != null )
        {
            q.setParameter( "name", "%" + name + "%" );
        }

        if( inn != null )
        {
            q.setParameter( "inn", inn );
        }
        if( ogrn != null )
        {
            q.setParameter( "ogrn", ogrn );
        }
        if( kpp != null )
        {
            q.setParameter( "kpp", kpp );
        }
        if( segment != null )
        {
            q.setParameter( "segment", segment );
        }
        return q.getResultList();
    }

    public Company findCompanyWithAllAttributes( Long id )
    {
        String hql = "select c from Company c left join fetch c.persons pc left join fetch pc.person p left join fetch p.contacts con " +
                "left join fetch c.addresses ad left join fetch ad.city ci left join fetch ci.country where c.id = :id";

        Query q = em.createQuery( hql );
        q.setParameter( "id", id );
        List<Company> companies = q.getResultList();
        if( companies.size() > 0  )
        {
            return companies.get( 0 );
        }
        else
        {
            return null;
        }
    }

    public Class getEntityClass()
    {
        return Company.class;
    }
}
